package tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import toolkit.Debug;
import form.Form;

public class FormTag extends AbstractTag
{
	private Tag tag;
	
	private PageContext pageContext;
	
	private String formId;
	
	public FormTag()
	{
		Debug.log("(" + id + ") FormTag()");
	}
	
	@Override
	public int doStartTag() throws JspException
	{
		Debug.log("(" + id + ") FormTag.doStartTag()");
		
		try
		{
			if (formId != null)
			{
				Form form = Form.getForm(formId);
				
				if (form != null)
				{
					pageContext.getOut().print(form);
				}
			}
		}
		catch (IOException e)
		{
			Debug.log(e);
		}
		
		return EVAL_PAGE;
	}
	
	@Override
	public int doEndTag() throws JspException
	{
		Debug.log("(" + id + ") FormTag.doEndTag()");
		
		return SKIP_BODY;
	}
	
	@Override
	public void setParent(Tag tag)
	{
		Debug.log("(" + id + ") FormTag.setParent(" + tag + ")");
		
		this.tag = tag;
	}
	
	@Override
	public Tag getParent()
	{
		Debug.log("(" + id + ") FormTag.getParent()");
		
		return tag;
	}
	
	@Override
	public void setPageContext(PageContext pageContect)
	{
		Debug.log("(" + id + ") FormTag.setPageContext(" + pageContect + ")");
		
		this.pageContext = pageContect;
	}
	
	@Override
	public void release()
	{
		Debug.log("(" + id + ") FormTag.release()");
	}
	
	public void setId(String formId)
	{
		Debug.log("(" + id + ") FormTag.setId(" + id + ")");
		
		this.formId = formId;
	}
	
	@Override
	protected void finalize() throws Throwable
	{
		Debug.log("(" + id + ") FormTag.finalize()");
	}
}
